package com.yuanjin.util;


import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanjin.R;

/**
 * 自定义Toast
 * 
 */
@SuppressLint("ShowToast")
public class ToastHelper {
	public static final int LENGTH_SHORT= 0;
	
	public static final int LENGTH_LONG = 1;
	//消息显示类型
	public static final int MESSAGE_TYPE_SUCCEED = 0;//成功消息
	public static final int MESSAGE_TYPE_FAILURE = 1;//失败消息
	private static Toast toast_msg;

	private static TextView textView;
	
	/**
	 * Make a standard toast that just contains a text view.
	 * 
	 * @param context
	 *            The context to use. Usually your
	 *            {@link android.app.Application} or
	 *            {@link android.app.Activity} object.
	 * @param text
	 *            The text to show. Can be formatted text.
	 * @param duration
	 *            How long to display the message. Either {@link #LENGTH_SHORT}
	 *            or {@link #LENGTH_LONG}
	 * 
	 */
	public static Toast makeText(Context context, int icon_id, int background, CharSequence text, int duration) {
		toast_msg = Toast.makeText(context, null, duration);
		toast_msg.setDuration(duration);
		toast_msg.setText(text);
		if (background > 0) {
			LinearLayout linearLayout = new LinearLayout(context);
			linearLayout.setPadding(20, 20, 20, 20);
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);
			linearLayout.setBackgroundResource(background);
			linearLayout.setGravity(Gravity.CENTER);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			linearLayout.setLayoutParams(layoutParams);
			
			ImageView imageView = new ImageView(context);
			if(icon_id >0 ){
				imageView.setImageResource(icon_id);
			}
			linearLayout.addView(imageView, layoutParams);
			
			textView = new TextView(context);
			textView.setText(text);
			layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(20, 0, 0, 0);
			textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
			linearLayout.addView(textView, layoutParams);
			
			toast_msg.setView(linearLayout);
			DisplayMetrics dm = context.getResources().getDisplayMetrics();
			((WindowManager) context.getSystemService(Service.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
			toast_msg.setGravity(Gravity.CENTER, 0, 0);
		}
		return toast_msg;
	}
	
//	public static Toast makeProgressText(Context context, float scale, int duration) {
//		toast_msg = Toast.makeText(context, null, duration);
//		toast_msg.setDuration(duration);
////		toast_msg.setText(text);
////		if (R.drawable.toast_bg > 0) {
//			LinearLayout linearLayout = new LinearLayout(context);
//			linearLayout.setPadding(20, 20, 20, 20);
//			linearLayout.setOrientation(LinearLayout.VERTICAL);
////			linearLayout.setBackgroundResource(R.drawable.toast_bg);
//			linearLayout.setGravity(Gravity.CENTER);
//			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			linearLayout.setLayoutParams(layoutParams);
//			
//			ImageView imageView = new ImageView(context);
//			if(R.drawable.video_volumn_bg >0 ){
//				imageView.setImageResource(R.drawable.video_volumn_bg);
//			}
//			linearLayout.addView(imageView, layoutParams);
//			
////			<ImageView
////            android:id="@+id/operation_percent"
////            android:layout_width="0dip"
////            android:layout_height="wrap_content"
////            android:layout_gravity="left"
////            android:scaleType="matrix"
////            android:src="@drawable/video_num_front" />
//			
//			ImageView imageViewNum = new ImageView(context);
//			imageViewNum.setScaleType(ScaleType.MATRIX);
//			imageViewNum.setImageResource(R.drawable.video_num_bg);
//			
//			ImageView imageViewFull = new ImageView(context);
//			imageViewNum.setScaleType(ScaleType.MATRIX);
//			imageViewNum.setImageResource(R.drawable.video_num_front);
//			
//			layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			layoutParams.setMargins(10, 0, 0, 0);
//			
//			LinearLayout.LayoutParams layoutParamsFull = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			layoutParamsFull.setMargins(10, 0, 0, 0);
//			layoutParamsFull.width = (int) (layoutParamsFull.width * scale);
//			
//			linearLayout.addView(imageViewNum, layoutParams);
//			linearLayout.addView(imageViewFull, layoutParamsFull);
//			
//			toast_msg.setView(linearLayout);
//			DisplayMetrics dm = context.getResources().getDisplayMetrics();
//			((WindowManager) context.getSystemService(Service.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
//			toast_msg.setGravity(Gravity.CENTER, 0, 0);
////		}
//		return toast_msg;
//	}
	
	public static Toast makeText(Context context, int icon_id,String message, int duration) throws Resources.NotFoundException {
		return makeText(context,icon_id, R.drawable.toast_bg, message, duration);
	}
	
	public static Toast makeText(Context context, int icon_id,int messageId, int duration) throws Resources.NotFoundException {
		return makeText(context,icon_id, R.drawable.toast_bg, context.getResources().getString(messageId), duration);
	}
	
	public static Toast makeText(Context context, int strSource, int duration) throws Resources.NotFoundException {
		return makeText(context,R.drawable.toast_icon_right, R.drawable.toast_bg, context.getResources().getString(strSource), duration);
	}
	
	public static Toast makeText(Context context, String message, int duration) throws Resources.NotFoundException {
		return makeText(context, R.drawable.toast_icon_right, R.drawable.toast_bg, message, duration);
	}
	
	public static void makeTextShow(Context context, int icon_id,String message, int duration) throws Resources.NotFoundException {
		makeText(context,icon_id, R.drawable.toast_bg, message, duration).show();
	}
	
	public static void makeTextShow(Context context, int icon_id,int messageId, int duration) throws Resources.NotFoundException {
		makeText(context,icon_id, R.drawable.toast_bg, context.getResources().getString(messageId), duration).show();
	}
	
	public static void makeTextShow(Context context, int strSource, int duration) throws Resources.NotFoundException {
		makeText(context,R.drawable.toast_icon_right, R.drawable.toast_bg, context.getResources().getString(strSource), duration).show();
	}
	
	public static void makeTextShow(Context context, String message, int duration) throws Resources.NotFoundException {
		makeText(context, R.drawable.toast_icon_right, R.drawable.toast_bg, message, duration).show();
	}
	public static void makeTextShowFail(Context context, String message, int duration) throws Resources.NotFoundException {
		makeText(context, R.drawable.toast_icon_fail, R.drawable.toast_bg, message, duration).show();
	}
	public static void makeTextShowFail(Context context, int strSource, int duration) throws Resources.NotFoundException {
		makeText(context, R.drawable.toast_icon_fail, R.drawable.toast_bg, context.getResources().getString(strSource), duration).show();
	}
	
	
}

